package com.epam.kulibaba.helpers.testSupport;

import java.util.ArrayList;
import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.testng.Assert;
import org.testng.Reporter;

import com.epam.kulibaba.components.PanelCriterias.PanelCornerCriterias;
import com.epam.kulibaba.components.catalog.ProductsCatalog;
import com.epam.kulibaba.components.catalog.navigator.CatalogNavigatorPager;
import com.epam.kulibaba.components.catalog.order.LinksBarOrder;
import com.epam.kulibaba.helpers.Class.Catalog;
import com.epam.kulibaba.helpers.Class.CatalogPage;
import com.epam.kulibaba.helpers.Class.Product;
import com.google.common.collect.Lists;

public class ProductsListHelper {

	public static List<Product> getProducts(ProductsCatalog productCatalog) {
		List<Product> products = Lists.newArrayList();
		List<WebElement> itemProducts = productCatalog.getCatalog()
				.findElements(By.cssSelector(".item"));
		for (WebElement item : itemProducts) {
			products.add(ConvertItemToProduct(item));
		}
		return products;
	}

	private static Product ConvertItemToProduct(WebElement item) {
		Product product = new Product();
		product.name = item.findElement(By.cssSelector(".name a")).getText();
		String price = item.findElement(By.cssSelector(".price strong"))
				.getText();
		price = price.replaceFirst("грн", "").replaceFirst(" ", "");
		product.price = Double.parseDouble(price);
		return product;
	}

	public static void goToProductPage(ProductsCatalog productCatalog,
			int productNumber) {
		Reporter.log("In products catalog go to product number "
				+ productNumber + " page");
		List<WebElement> product = productCatalog.getCatalog().findElements(
				By.xpath(".//div[@class='name']/a"));
		product.get(productNumber - 1).click();		
	}

	public static String getLinkOnProductPage(ProductsCatalog productCatalog,
			int itemNumber) {
		Reporter.log("In products catalog get link on product " + itemNumber
				+ " page");
		List<WebElement> product = productCatalog.getCatalog().findElements(
				By.xpath(".//div[@class='name']/a"));
		String productHref = product.get(itemNumber - 1).getAttribute("href");
		System.out.println(productHref);
		return productHref;
	}
	
	public static String getNameToSearchProduct(ProductsCatalog productCatalog,
			int itemNumber) {
		Reporter.log("In products catalog get link on product " + itemNumber
				+ " page");
		List<WebElement> product = productCatalog.getCatalog().findElements(
				By.xpath(".//div[@class='name']/a"));
		String productNameToSearch = product.get(itemNumber - 1).getText();
		System.out.println(productNameToSearch);
		return productNameToSearch;
	}
	
	public static void addProductToCompare(ProductsCatalog productsCatalog,
			int productNumber) {
		Reporter.log("In products catalog add product number " + productNumber
				+ " to Compare");
		productsCatalog.getCatalog()
				.findElement(By.xpath("//div[@class='item']['productNumber']"))
				.findElement(By.xpath("//span[@rel='0']")).click();
	}

	public static void removeProductFromCompare(
			ProductsCatalog productsCatalog, int productNumber) {
		Reporter.log("Remove product" + productNumber + " from Compare");
		productsCatalog
				.getCatalog()
				.findElement(
						By.xpath("(//div[@class='item'])['productNumber']"))
				.findElement(By.xpath("//span[@rel='1']")).click();
	}

	public static void goToProductsCompare(ProductsCatalog productsCatalog,
			int productNumber) {
		Reporter.log("Go to products " + productNumber + " compare");
		productsCatalog.getCompareLink().click();
	}

	public static void sortProductsListByPrice(LinksBarOrder linkSortByPrice) {
		Reporter.log("Clik on link sortByPrice");
		linkSortByPrice.getButtonSortByPrice().click();
	}

	public static void sortProductsListByName(LinksBarOrder linkSortByName) {
		Reporter.log("Clik on link sortByName");
		linkSortByName.getButtonSortByName().click();
	}

	public static void goToForwardPage(CatalogNavigatorPager linkForward) {
		Reporter.log("Go to forward page in products catalog");
		linkForward.getButtonForward().click();
	}

	public static void goToFirstPage(CatalogNavigatorPager firstPage) {
		Reporter.log("Go to first page in products catalog");
		firstPage.getFirstPage().click();
	}

	public static Catalog getAllProductsList(WebDriver driver,
			CatalogNavigatorPager catalogPager,
			CatalogNavigatorPager linkForward, ProductsCatalog productsCatalog) {
		Reporter.log("Get All products list");
		String lp = catalogPager.getLastPage().getText();
		Catalog catalog = new Catalog();
		for (int index = 0; index < Integer.parseInt(lp); index++) {
			if (index > 0)
				goToForwardPage(linkForward);
			productsCatalog = ProductsCatalog.getProductsCatalog(driver);
			CatalogPage catalogPage = new CatalogPage();
			catalogPage.produtcs = ProductsListHelper
					.getProducts(productsCatalog);
			catalog.pages.add(catalogPage);
			catalog.globals.addAll(catalogPage.produtcs);
		}

		return catalog;
	}

	public static void checkSortProductsListByPrice(Catalog catalog) {
		Reporter.log("check Sort Products List By Price");
		List<Product> myProducts = new ArrayList<Product>(catalog.globals);
		SortingEngine.orderBy("price", myProducts);
		for (int i = 0; i < myProducts.size(); i++) {
			Assert.assertEquals(catalog.globals.get(i).getPrice(), myProducts
					.get(i).getPrice(), 0.01);
		}
	}

	public static void checkSortProductsListByName(Catalog catalog) {
		Reporter.log("check Sort Products List By Name");
		List<Product> myProducts = new ArrayList<Product>(catalog.globals);
		SortingEngine.orderBy("name", myProducts);
		for (int i = 0; i < myProducts.size(); i++) {
			Assert.assertEquals(catalog.globals.get(i).getName(), myProducts
					.get(i).getName());
		}
	}

	public static void setPriceFilter(CatalogNavigatorPager firstPage,
			PanelCornerCriterias minPrices, PanelCornerCriterias maxPrices,
			double minPrice, double maxPrice) {
		// TODO: minPrice<maxPrice
		ProductsListHelper.goToFirstPage(firstPage);
		PanelCriteriasHelper.setMinPrice(minPrices, minPrice);
		PanelCriteriasHelper.setMaxPrice(maxPrices, maxPrice);
	}

	public static int filterPriceCatalog(Catalog catalog, double minPrice,
			double maxPrice) {
		Reporter.log("Sort products in catalog between minimum " + minPrice
				+ " and maximum prices" + maxPrice);
		List<Product> myProducts = new ArrayList<Product>(catalog.globals);
		List<Product> myFilterProducts = new ArrayList<Product>();
		for (Product item : myProducts) {
			if (item.getPrice() >= minPrice && item.getPrice() <= maxPrice) {
				myFilterProducts.add(item);
			}
		}
		// System.out.println("myFilterProducts size = "
		// +myFilterProducts.size());
		return myFilterProducts.size();
	}

	public static void checkFilterPriceProducts(Catalog catalog,
			WebDriver driver, CatalogNavigatorPager catalogPager,
			CatalogNavigatorPager linkForward, ProductsCatalog productsCatalog,
			double minPrice, double maxPrice, PanelCornerCriterias total,
			CatalogNavigatorPager firstPage, PanelCornerCriterias minPrices,
			PanelCornerCriterias maxPrices) {
		catalog = getAllProductsList(driver, catalogPager, linkForward,
				productsCatalog);
		setPriceFilter(firstPage, minPrices, maxPrices, minPrice, maxPrice);
		Reporter.log("Check the correct sort products between minimum "
				+ minPrice + " and maximum prices" + maxPrice);
		Assert.assertEquals(filterPriceCatalog(catalog, minPrice, maxPrice),
				Integer.parseInt(total.getTotal().getText()));
	}

	public static List<String> getProductsShortDescription(
			ProductsCatalog productsCatalog) {
		Reporter.log("Get products short description");
		List<String> productsShortDescription = new ArrayList<String>();
		List<WebElement> itemProducts = productsCatalog.getCatalog()
				.findElements(By.cssSelector(".item"));
		for (WebElement item : itemProducts) {
			productsShortDescription.add(item
					.findElement(By.cssSelector(".description")).getText()
					.toLowerCase());
		}
		System.out.println(productsShortDescription);
		return productsShortDescription;
	}

	public static List<String> getAllProductsListShortDescription(
			WebDriver driver, CatalogNavigatorPager catalogPager,
			CatalogNavigatorPager linkForward, ProductsCatalog productsCatalog) {
		String lp = catalogPager.getLastPage().getText();
		List<String> productsShortDescription = new ArrayList<String>();
		for (int index = 0; index < Integer.parseInt(lp); index++) {
			if (index > 0)
				goToForwardPage(linkForward);
			List<WebElement> itemProducts = productsCatalog.getCatalog()
					.findElements(By.cssSelector(".item"));
			for (WebElement item : itemProducts) {
				productsShortDescription.add(item
						.findElement(By.cssSelector(".description")).getText()
						.toLowerCase());
			}
			System.out.println(productsShortDescription);
		}
		return productsShortDescription;

	}

}
